$(function(){
	$("body").prepend("<div id=\"controlpanel\"></div>");
	$("#controlpanel").append("<div id=\"controlheader\"><ul><li class=\"logo\"></li><li><a href=\"\" class=\"current\">开始</a><a href=\"\">网页布局</a><a href=\"\">内容管理</a><a href=\"\">资源库</a><a href=\"\">设置</a></li><li class=\"rightmenu\"><a href=\"\" class=\"put\">发布</a><a href=\"\">预览</a><a href=\"\">还原</a><a href=\"\">备份</a><a href=\"\" class=\"m\">honfei,</a><a href=\"\">退出</a><p><a href=\"\">修改密码</a><a href=\"\">我的资料</a><a href=\"\">账户管理</a></p></li></ul></div>");
	$("#controlpanel").append("<div id=\"controlcontent\"></div>");
	$("#controlpanel").append("<div class=\"closebtn\">【<a href=\"\">关闭</a>】</div>");
	$("#controlcontent").load("/apps/Admin/Tpl/Public/layout.html");
	$("#controlheader ul li a.m").hover(function(){
		$("#controlheader ul li.rightmenu p").hover(function(){			
			$(this).show();
		},function(){
			$(this).hide();
		}).show();
	},function(){
		$("#controlheader ul li.rightmenu p").hide();
	});
	$("a").bind("focus",function(){if(this.blur)this.blur();});//去除链接虚线
	//菜单
	$("#controlheader ul:first li:eq(1) a").click(function(e){
		$(this).siblings().removeClass("current");
		$(this).addClass("current");
		$("#controlcontent,div.closebtn").show();
		e.preventDefault();
		
		$("#Placeholder").remove();
		$("body").prepend("<div id=\"Placeholder\" style=\"height:"+($("#controlpanel").height()-$("#controlheader").height())+"px\"></div>");
		
	});
	$("#controlpanel div.closebtn a").bind("click",function(e){
		$("#controlcontent,div.closebtn").hide();
		e.preventDefault();
		$("#Placeholder").remove();
		
	});
	//布局拖动
	//$(".toolbox li a").click(function(e){e.preventDefault();});
	
	$( ".toolbox li a" ).livequery(function(){
		$(this).draggable({
			appendTo: "body",
			cursorAt: { top: -3, left: -3 }, //位置定位坐标设置
			opacity:"0.6",
			helper: function(){
				return $("<div style=\"width:360px;height:28px;background:#8EF4FE;border:1px #0A9CFC dashed;z-index:3000;padding-top:5px\"><div style=\"background:#CBF8FA;height:100%;width:100%\"></div></div>");
			}
		});
		
	});		
	$( ".column" ).droppable({
		//activeClass: "ui-state-default",
		//hoverClass: "ui-state-hover",
		accept: ":not(.widget)",		
		drop: function( event, ui) {
			var result;
			$.get("/index.php/admin/public/loadLayout",{layout:$(ui.draggable).attr('id')},function(obj){
				if(obj.status){
					result = obj.data;
				}
				
			},"json");
	
			$result = $("<div class=\"widget\"><div class=\"widget-header\">架构33</div><div class=\"widget-content\">内容区</div></div>");
	
			$(this).find(".placeholder").remove();	
			$result.addClass( "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
			.find( ".widget-header" ).addClass( "ui-widget-header ui-corner-all" )
			.prepend( "<span class='ui-icon-btn'>编辑</span>").end().find( ".widget-content" );
			$result.resizable({containment: ".column"}).appendTo(this);
			
			
			//$( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
		}
	}).sortable({
		//items: "li:not(.placeholder)",
		stop: function() {
			//alert($(document).html());
		}
	});
	
	
	//页面布局
	$(".column").sortable({
		accept: ".contentlayout",
		forcePlaceholderSize: 'true',
		connectWith: ".column",
		helper: "clone",
		opacity: 0.7
	});
	$(".widget").addClass( "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
	.find( ".widget-header" ).addClass( "ui-widget-header ui-corner-all" )
	.prepend( "<span class='ui-icon-btn'>编辑</span>").end().find( ".widget-content" );
	$(".widget" ).resizable({containment: ".column"});
	$(".ui-icon-btn" ).live("click",function(e) {
		$("<div class=\"widget-menu\"><ul><li>标题</li><li>样式</li><li>数据</li><li>删除</li></ul></div>").appendTo("body");
		$(".widget-menu").css({"top":$(this).offset().top,"left":$(this).offset().left-17})
		.hover(function(){
			return;
		},function(){
			$(this).remove();
		});
		
	});
	$( ".column" ).disableSelection();
	
});
